package com.dries.portal.module.coupon.utils.convert;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.dries.portal.module.coupon.enums.CouponCategory;
import com.dries.portal.module.coupon.enums.DistributeTarget;
import com.dries.portal.module.coupon.enums.ProductLine;
import com.dries.common.utils.DateUtils;
import com.dries.portal.module.coupon.entity.Template;
import com.dries.portal.module.coupon.form.AddTemplateForm;
import org.springframework.beans.BeanUtils;

import java.time.LocalDateTime;

/**
 * @Description:
 * @Author ZSY
 * @createTime 2020/7/27 17:31
 */
public class AddTemplateForm2TemplateConvert {

    public static Template convert(AddTemplateForm form){

        Template template = new Template();
        BeanUtils.copyProperties(form, template);

        template.setName(form.getName()+ "-" + DateUtils.formatterYMDHMS.format(LocalDateTime.now()));
        template.setCategory(CouponCategory.of(form.getCategory()));
        template.setProductLine(ProductLine.of(form.getProductLine()));
        template.setTarget(DistributeTarget.of(form.getTarget()));
        template.setRule(JSONUtil.toJsonStr(form.getRule()));
        template.setAvailable(0);
        template.setExpired(0);

        // 生成唯一的Template_key
        template.setTemplateKey(RandomUtil.randomNumbers(6) + System.currentTimeMillis());

        return template;

    }

}
